import type {
  IConversation,
} from "@src/types";
import useStore from "@src/store/store";

export const timeformat  = (time:string) => {
  var date = new Date(time);
  var day = date.getDate();
  var hours = date.getHours();
  var minutes = date.getMinutes();

  var formattedTime = day.toString() + '日 ' + hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0');
  return formattedTime;

}

export const isToday = (time:string) => {

  var now = new Date();
  var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

  var date = new Date(time);

  // 比较时间
  if (date.toDateString() === today.toDateString()) {
    return true;
  } else {
    return false;
  }
}

export const isYestoday = (time:string) => {
  var now = new Date();
  var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());

  var date = new Date(time);
  var yesterday = new Date(today);
  yesterday.setDate(today.getDate() - 1);

  // 比较时间
  if (date.toDateString() === yesterday.toDateString()) {
    return true;
  } else {
    return false;
  }
}




/**
 * trim a string when it reaches a certain length and adds three dots
 * at the end.
 * @param text
 * @param maxLength
 * @returns A string that is trimmed according the length provided
 */
export const shorten = (message:  string, maxLength: number = 23) => {
  let text: string | undefined;

  if (typeof message === "string") {
    text = message;
  } 

  if (text && typeof text === "string") {
    let trimmedString = text;
    if (text.length > maxLength) {
      // trim the string to the maximum length.
      trimmedString = trimmedString.slice(0, maxLength);
      // add three dots to indicate that there is more to the message.
      trimmedString += "...";
    }
    return trimmedString;
  }

  return "";
};



/**
 * Convert unicode to native emoji
 *
 * @param unicode - emoji unicode
 */
export const unicodeToEmoji = (unicode: string) => {
  return unicode
    .split("-")
    .map((hex) => parseInt(hex, 16))
    .map((hex) => String.fromCodePoint(hex))
    .join("");
};
